package com.intretech.tj.live.service

import jakarta.enterprise.context.ApplicationScoped


@ApplicationScoped
class Live0Service {

    private companion object {
        private val PATH_1 = intArrayOf(1, 3, 43, 44, 53, 54, 90, 17, 27, 29, 2)
        private val PATH_2 = intArrayOf(54, 55, 56, 58, 59, 88, 61, 38, 36, 39, 47, 48, 49, 43, 44, 53)
        private val PATH_3 = intArrayOf(
            15, 27, 29, 2, 13, 28, 30, 31, 32, 70, 71, 68, 69, 25, 21, 19, 18
        )
        private val PATH_4 = intArrayOf(25, 21, 19, 18, 32, 35, 38, 36, 39, 37, 30, 29, 2, 1, 4, 87, 85, 82, 67, 69)

        private val PATH_5 = intArrayOf(
            66, 83, 84, 86, 5, 7, 8, 11, 93, 12, 14, 74, 22, 26, 79, 78
        )

        // 公交车
        private val PATH_BUS = intArrayOf(
            46, 48, 49, 43, 44, 53, 54, 90, 17, 14, 74, 77, 75, 81, 80, 76, 78, 66, 83, 84, 86, 5, 3, 43, 44, 45
        )

    }

    /**
     * ROS1 闲置情景
     * 根据传入的当前节点ID，在预设路径中查找，并返回其后第二个节点ID。
     * 如果目标索引越界，则从数组头部重新开始计算。
     *
     * @param currentNodeId 当前节点ID。
     * @return 如果找到，则返回目标节点ID；如果 currentNodeId 不在路径中，则返回-1。
     */
    fun live0ROS(rosDeviceID: String, currentNodeId: Int): Int {
        val currentPath = when (rosDeviceID) {
            "ROS1" -> PATH_1
            "ROS2" -> PATH_2
            "ROS3" -> PATH_3
            "ROS4" -> PATH_4
            "ROS5" -> PATH_5
            "ROS_BUS" -> PATH_BUS
            else -> intArrayOf()
        }

        // 查找 currentNodeId 在数组中的索引
        val index = currentPath.indexOf(currentNodeId)

        // 如果未找到 currentNodeId，直接返回 -1
        if (index == -1) {
            return -1
        }

        // 计算目标索引，使用取模运算符 (%) 实现循环
        // 如果 index + 2 超出数组范围，它会自动从头开始
        val targetIndex = (index + 2) % currentPath.size

        return currentPath[targetIndex]
    }
}